package com.qfedu.mtl.dao.impl;

import com.qfedu.mtl.dao.SystemparamDao;
import com.qfedu.mtl.dto.SystemparamDto;
import com.qfedu.mtl.entity.Systemparam;
import com.qfedu.mtl.query.SystemparamQuery;
import com.qfedu.mtl.util.DBUtil;
import com.qfedu.mtl.util.StrUtil;
import com.qfedu.mtl.util.handler.impl.BeanHandler;
import com.qfedu.mtl.util.handler.impl.BeanListHandler;
import com.qfedu.mtl.util.handler.impl.NumberHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * @version v1.0
 * @Author NVSonar
 * @Date 2023/9/26 21:41
 * @Email 1595220980@qq.com
 */
public class SystemparamDaoImpl implements SystemparamDao {
	@Override
	public Systemparam selectById(Integer id) {
		String sql = "select * from tb_systemparam where systemparam_id=?";
		return DBUtil.executeDQL(sql, DBUtil.getConn(), new BeanHandler<>(Systemparam.class, true), id);
	}

	@Override
	public int insert(Systemparam obj) {
		String sql = "insert into tb_systemparam(systemparam_name,systemparam_value) values(?,?)";
		return DBUtil.executeDML(sql, DBUtil.getConn(), obj.getSystemparamName(), obj.getSystemparamValue());


	}

	@Override
	public int delete(Integer id) {
		String sql = "delete from tb_systemparam where systemparam_id=?";
		return DBUtil.executeDML(sql, DBUtil.getConn(), id);
	}


	@Override
	public void updateWebName(SystemparamDto obj) {
		String sql="update tb_systemparam set systemparam_value=? where systemparam_name='web_name'";
		DBUtil.executeDML(sql, DBUtil.getConn(),obj.getSitename());
	}

	@Override
	public void updateWebUrl(SystemparamDto obj) {
		String sql="update tb_systemparam set systemparam_value=? where systemparam_name='web_url'";
		DBUtil.executeDML(sql, DBUtil.getConn(),obj.getDomain());
	}

	@Override
	public void updateCacheTime(SystemparamDto obj) {
		String sql="update tb_systemparam set systemparam_value=? where systemparam_name='cache_time'";
		DBUtil.executeDML(sql, DBUtil.getConn(),obj.getCacheTime());
	}

	@Override
	public void updateFileMaxSize(SystemparamDto obj) {
		String sql="update tb_systemparam set systemparam_value=? where systemparam_name='file_max_size'";
		DBUtil.executeDML(sql, DBUtil.getConn(),obj.getCacheFileMax());
	}

	@Override
	public void updateUploadFileType(SystemparamDto obj) {
		String sql="update tb_systemparam set systemparam_value=? where systemparam_name='upload_file_type'";
		DBUtil.executeDML(sql, DBUtil.getConn(),obj.getCacheFileClass());
	}

	@Override
	public void updateHomepageTitle(SystemparamDto obj) {
		String sql="update tb_systemparam set systemparam_value=? where systemparam_name='homepage_title'";
		DBUtil.executeDML(sql, DBUtil.getConn(),obj.getTitle());
	}

	@Override
	public void updateMetaKeywords(SystemparamDto obj) {
		String sql="update tb_systemparam set systemparam_value=? where systemparam_name='meta_keywords'";
		DBUtil.executeDML(sql, DBUtil.getConn(),obj.getKeywords());
	}

	@Override
	public void updateMetaDescribe(SystemparamDto obj) {
		String sql="update tb_systemparam set systemparam_value=? where systemparam_name='meta_describe'";
		DBUtil.executeDML(sql, DBUtil.getConn(),obj.getDescript());
	}

	@Override
	public void updateCopyrightInformation(SystemparamDto obj) {
		String sql="update tb_systemparam set systemparam_value=? where systemparam_name='copyright_Information'";
		DBUtil.executeDML(sql, DBUtil.getConn(),obj.getCopyright());
	}



	@Override
	public List<Systemparam> selectAll() {
		String sql = "select * from tb_systemparam";
		return DBUtil.executeDQL(sql, DBUtil.getConn(), new BeanListHandler<>(Systemparam.class, true));
	}

	@Override
	public List<Systemparam> selectData(Integer page, Integer size, SystemparamQuery query) {
		List<Object> params = new ArrayList<>();
		StringBuilder sqlBuilder = new StringBuilder("select * from tb_systemparam where 1=1 ");
		if (!StrUtil.isNullOrEmpty(query.getSystemparamName())) {
			sqlBuilder.append(" and  systemparam_name = ? ");
			params.add(query.getSystemparamName());
		}
		sqlBuilder.append(" limit ?,?");
		params.add((page - 1) * size);
		params.add(size);
		return DBUtil.executeDQL(sqlBuilder.toString(), DBUtil.getConn(), new BeanListHandler<>(Systemparam.class, true), params.toArray());

	}

	@Override
	public Integer selectTotal(SystemparamQuery query) {
		String sql = "select count(*) from tb_systemparam where 1=1 ";
		return DBUtil.executeDQL(sql, new NumberHandler<>(Long.class)).intValue();
	}
}
